import {
    randomColor
} from '../../../assets/js/index'
let pie = {
    tooltip: {
        trigger: 'item',
        formatter: '{d}%,{b}'
    },
    graphic: { // 设置圆环图中间的文字
        type: 'text',
        left: 'center',
        top: 'middle',
        style: {
            text: '',
            textAlign: 'center',
            fill: '#000',
            font: '28px "Microsoft YaHei","Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB",Arial,sans-serif'
        }
    },
    color: [],
   /*  legend: {
         orient: 'horizontal',
         bottom: '0',
         itemWidth: 10,
         itemHeight: 10,
         selectedMode: false,
         data: []
    }, */
    series: [
        {
          type: 'pie',
          radius: ['45%', '80%'],
          avoidLabelOverlap: false,
          hoverOffset: 0,
          label: {
            normal: {
              show: false,
              position: 'center'
            },
            emphasis: {
              show: false
            }
          },
          labelLine: {
            normal: {
              show: false
            }
          },
          data: []
        }
    ]
}
export const dataFormat = (obj) => {
    /* list.forEach(item => {
        pie.style.text = item.title
    }) */
    let dataObj = {
        legends: [],
        options: []
    }
    if (obj) {
        pie.graphic.style.text = obj.title
        /* pie.series[0].data = obj.data.map(data => {
            return {
                name: data.name,
                value: data.value
            }
        })
        dataObj.legends = obj.data.map(data => {
            return {
                rate: data.rate,
                name: data.name
            }
        }) */
        let colors = []
        let datas = []
        let legends = []
        obj.data.forEach((data, ind) => {
            let tempColor = randomColor()
            let legendItem = {
                color: tempColor,
                rate: data.rate,
                name: data.name
            }
            let optionItem = {
                value: data.value,
                name: data.name
            }
            colors[ind] = tempColor
            datas[ind] = optionItem
            legends[ind] = legendItem
        })
        for (let i = 0; i < legends.length;) {
            let insertItem = {
                item1: legends[i],
                item2: legends[i + 1]
            }
            dataObj.legends.push(insertItem)
            i += 2
        }
        pie.series[0].data = datas
        pie.color = colors
        dataObj.options = pie
    }
    return dataObj
}
